package com.xry.user.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.User;
import com.xry.common.service.UserService;
import com.xry.common.util.PasswordUtil;
import com.xry.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86152
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-09-02 14:45:08
*/
@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Override
    public User getUserById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id).eq(User::getDeleted, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public User getUserByUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username).eq(User::getDeleted, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public boolean updateUserById(User user) {
        if (user.getPassword()!=null){
            user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        }
        return super.updateById(user);
    }

    @Override
    public boolean deleteUserById(Integer id) {
        User user = super.getById(id);
        if (user != null){
            user.setDeleted(1);
            return super.updateById( user);
        }
        return false;
    }

    @Override
    public boolean addUser(User user) {
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        return super.save(user);
    }

    @Override
    public List<User> getAll() {
        return super.list();
    }

    @Override
    public IPage<User> getPage(User user, IPage<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        // 只对非空字段进行模糊查询
        if (user != null) {
            if (StringUtils.hasText(user.getUsername())) {
                queryWrapper.like(User::getUsername, user.getUsername());
            }
            if (StringUtils.hasText(user.getEmail())) {
                queryWrapper.like(User::getEmail, user.getEmail());
            }
            if (StringUtils.hasText(user.getName())) {
                queryWrapper.like(User::getName, user.getName());
            }
            if (StringUtils.hasText(user.getPhone())) {
                queryWrapper.like(User::getPhone, user.getPhone());
            }
            if (StringUtils.hasText(user.getGender())) {
                queryWrapper.like(User::getGender, user.getGender());
            }
            if (user.getRoleid() != null) {
                queryWrapper.like(User::getRoleid, user.getRoleid());
            }
        }

        // 添加未删除条件
        queryWrapper.eq(User::getDeleted, 0);

        return super.page(page, queryWrapper);
    }

    @Override
    public User getByEmail(String email) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, email).eq(User::getDeleted, 0);
        return super.getOne(queryWrapper);
    }
}




